package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.Entity.Category;
import org.example.Entity.Dish;
import org.example.Entity.Setmeal;
import org.example.Mapper.CategoryMapper;
import org.example.common.CustomException;
import org.example.service.CategoryService;
import org.example.service.DishService;
import org.example.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    /**
     * 删除分类,删除之前判断是否关联了菜品和套餐
     * @param id
     */
    public void remove(Long id) {
        //判断是否关联菜品
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.eq(Dish::getCategoryId,id);
        int dishCount = dishService.count(dishQueryWrapper);
        if (dishCount > 0){
            //关联菜品，抛出业务异常
            throw new CustomException("当前分类下关联了菜品，不能删除!");
        }
        //判断是否关联套餐
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.eq(Setmeal::getCategoryId,id);
        int setmealCount = setmealService.count(setmealQueryWrapper);
        if (setmealCount > 0){
            //关联菜品，抛出业务异常
            throw new CustomException("当前分类下关联了菜品，不能删除!");
        }
        //执行删除(调用父类[ServiceImpl]的删除方法)
        super.removeById(id);

    }
}
